package com.flst.sql.db;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.flst.entity.Cours;
import com.flst.entity.Promotion;
import com.flst.sql.CoursDao;
import com.flst.sql.MaBaseSQLite;
import com.flst.sql.MatiereDao;
import com.flst.sql.ProfesseurDao;
import com.flst.sql.PromotionDao;
import com.flst.sql.SalleDao;

public class CoursDaoDb implements CoursDao {

	private SQLiteDatabase base;
	private MaBaseSQLite maBase;

	private static final String TABLE = "T_Cours";
	private static final String COL_ID = "ID";
	private static final String COL_DATE = "date";
	private static final String COL_H_DEBUT = "heure_debut";
	private static final String COL_H_FIN = "heure_fin";
	private static final String COL_PROFESEUR = "id_professeur";
	private static final String COL_MATIERE = "id_matiere";
	private static final String COL_PROMOTION = "id_promotion";
	private static final String COL_SALLE = "id_salle";

	public CoursDaoDb(MaBaseSQLite bdd) {
		base = bdd.getReadableDatabase();
		maBase = bdd;
	}

	@Override
	public List<Cours> getAllCours() {

		List<Cours> list_cours = new ArrayList<Cours>();

		Cursor c = base.query(TABLE, new String[] { COL_ID, COL_DATE,
				COL_H_DEBUT, COL_H_FIN, COL_PROFESEUR, COL_MATIERE,
				COL_PROMOTION, COL_SALLE }, null, null, null, null, null);

		if (c.moveToFirst()) {
			do {
				list_cours.add(cursorToCours(c));
			} while (c.moveToNext());
		}
		c.close();

		return list_cours;
	}

	@Override
	public List<Cours> getCoursByPromotino(Promotion promotion) {
		List<Cours> list_cours = new ArrayList<Cours>();

		Cursor c = base.query(TABLE, new String[] { COL_ID, COL_DATE,
				COL_H_DEBUT, COL_H_FIN, COL_PROFESEUR, COL_MATIERE,
				COL_PROMOTION, COL_SALLE }, COL_PROMOTION + "=" + promotion.getId() , null, null, null, null);
//+ " ORDER BY date DESC, heure_debut"
		if (c.moveToFirst()) {
			do {
				list_cours.add(cursorToCours(c));
			} while (c.moveToNext());
		}
		c.close();
		
		return list_cours;
	}
	
	@Override
	public List<Cours> getCoursByPromotionAndDate(Promotion promotion,
			String date) {
		List<Cours> list_cours = new ArrayList<Cours>();

		Cursor c = base.query(TABLE, new String[] { COL_ID, COL_DATE,
				COL_H_DEBUT, COL_H_FIN, COL_PROFESEUR, COL_MATIERE,
				COL_PROMOTION, COL_SALLE }, COL_PROMOTION + "=" + promotion.getId() , null, null, null, null);
//+ " ORDER BY date DESC, heure_debut"
		if (c.moveToFirst()) {
			do {
				list_cours.add(cursorToCours(c));
			} while (c.moveToNext());
		}
		c.close();
		
		return list_cours;
	}

	public Cours cursorToCours(Cursor c) {

		ProfesseurDao professeurDao = new ProfesseurDaoDb(maBase);
		MatiereDao matiereDao = new MatiereDaoDb(maBase);
		PromotionDao promotionDao = new PromotionDaoDb(maBase);
		SalleDao salleDao = new SalleDaoDb(maBase);

		Cours cours = new Cours();

		cours.setId(c.getLong(0));
		
//		StringTokenizer tokenizer = new StringTokenizer(c.getString(1),"/");
//		String day = tokenizer.nextToken();
//		String month = tokenizer.nextToken();
//		String year = tokenizer.nextToken();
		
		cours.setDate(c.getString(1));
		
		cours.setHeure_debut(c.getString(2));
		cours.setHeure_fin(c.getString(3));
		cours.setProfesseur(professeurDao.getProfesseurById(c.getLong(4)));
		cours.setMatiere(matiereDao.getMatiereById(c.getLong(5)));
		cours.setPromotion(promotionDao.getPromotionById(c.getLong(6)));
		cours.setSalle(salleDao.getSalleById(c.getLong(7)));

		return cours;
	}

}
